import { defineStore } from "pinia";
import { HOME_MODULES_TYPE as MODULES_TYPE } from "@/store";

export * from "./modules";

/**
 * 应用程序类型定义
 * @property label - 应用程序的标签名称
 */
export type ModuleType = {
    label: string;
    icon: string;
    mode: string;
    url: string;
};

type ModuleItemType = {
    label: string;
    modules: ModuleType[];
};

/**
 * 应用程序存储状态类型定义
 * @property modules - 应用程序列表
 */
type AppsStoreType = {
    home: ModuleItemType;
};

export const useModuleStore = defineStore("modules", {
    /**
     * 初始化存储状态
     * @returns 返回初始状态对象，包含空的应用程序数组
     */
    state: (): AppsStoreType => ({
        home: {
            label: "首页",
            modules: [
                {
                    label: "我的宠物",
                    icon: "icon-health",
                    url: "",
                    mode: MODULES_TYPE.HomeModulePet
                },
                {
                    label: "我的待办",
                    icon: "icon-health",
                    url: "",
                    mode: MODULES_TYPE.HomeModuleTodo
                },
                {
                    label: "记账助手",
                    icon: "icon-health",
                    url: "",
                    mode: MODULES_TYPE.HomeModuleBillRecord
                },
                {
                    label: "体重管理",
                    icon: "icon-health",
                    url: "",
                    mode: MODULES_TYPE.HomeModuleWeight
                },
                {
                    label: "健康日历",
                    icon: "icon-health",
                    url: "",
                    mode: MODULES_TYPE.HomeModuleCalendar
                }
            ]
        }
    }),

    actions: {
        updateHomeModule(module: ModuleType[]) {
            this.home.modules = module;
        }
    },

    /**
     * 配置数据持久化策略
     * 启用数据持久化功能，根据当前运行模式生成唯一的存储键名
     */
    persist: {
        enabled: true,
        strategies: [
            {
                key: import.meta.env.MODE + "-module-store"
            }
        ]
    }
});
